<?php

namespace app\pilot\controller;

use app\pilot\model\BalanceRecordModel;
use app\pilot\model\PilotModel;
use app\pilot\model\ToCashModel;
use think\admin\Controller;
use think\admin\service\AdminService;
use think\Exception;
use think\facade\Db;


/**
 * 飞手提现管理
 * @class ToCash
 * @package app\pilot\controller
 */
class ToCash extends Controller
{
    /**
     * 飞手提现列表
     * @auth true
     * @menu true
     */
    public function index()
    {
        $this->title = '飞手提现列表';
        ToCashModel::mQuery()->with('pilot')
            ->like('nickname,realname')
            ->equal('status')
            ->dateBetween('create_at')
            ->order('id desc')
            ->layTable();
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_page_filter(array &$data)
    {

    }

    /**
     * 飞手提现详情
     * @auth true
     */
    public function detail()
    {
        $this->vo = ToCashModel::with('pilot')->where('id', $this->request->get('id'))->find();
        $this->fetch();
    }

    /**
     * 添加飞手提现
     * @auth true
     */
    public function add()
    {
//        $this->_applyFormToken();
        ToCashModel::mForm('form');
    }

    /**
     * 编辑飞手提现
     * @auth true
     */
    public function edit()
    {
//        $this->_applyFormToken();
        ToCashModel::mForm('form');
    }

    /**
     * 表单数据处理
     * @param array $vo
     */
    protected function _form_filter(array &$vo)
    {
        if ($this->request->isGet()) {

        }else{

        }
    }

    /**
     * 审核飞手提现
     * @auth true
     */
    public function review()
    {
        if ($this->request->isGet()){
            $this->vo = ToCashModel::with('pilot')->where('id', $this->request->get('id'))->find();
            $this->fetch();
        }else{
            $id = $this->request->post('id');
            $status = $this->request->post('status');
            $review_reason = $this->request->post('review_reason');

            Db::startTrans();
            try {
                $to_cash = ToCashModel::where('id', $id)->lock(true)->find();
                if (empty($to_cash)) $this->error('飞手提现申请不存在');
                if ($to_cash->status != 0) $this->error('待审核的申请才能审核');
                if (!in_array($status, [1, -1])) $this->error('审核状态不正确');
                $to_cash->save([
                    'status' => $status,
                    'review_reason' => $review_reason,
                    'review_at' => date('Y-m-d H:i:s'),
                    'review_admin_id' => AdminService::getUserId(),
                ]);
                // 如果审核不通过，恢复飞手的余额
                if ($status == -1){
                    $pilot = PilotModel::where('is_deleted', 0)->where('id', $to_cash->pilot_id)->lock(true)->find();
                    // 恢复余额
                    BalanceRecordModel::create([
                        'pilot_id' => $pilot->id,
                        'type' => BalanceRecordModel::TYPE__IN_WITHDRAW_BACK,
                        'amount' => $to_cash->money,
                        'before' => $pilot->balance,
                        'after' => $pilot->balance + $to_cash->money,
                        'remark' => '',
                    ]);
                    $pilot->balance += $to_cash->money;
                    $pilot->save();
                }
                Db::commit();
            }catch (Exception $e){
                Db::callback();
                $this->error($e->getMessage());
            }
            $this->success('保存成功');
        }
    }
}
